package de.ret_world.openrpgstudio.mapeditor;

import buoy.event.CommandEvent;
import buoy.event.ValueChangedEvent;
import buoy.event.WindowClosingEvent;
import buoy.widget.*;
import de.ret_world.openrpgstudio.editor.Media;
import de.ret_world.openrpgstudio.editor.Project;
import de.ret_world.openrpgstudio.editor.gui.ImagePreviewWidget;
import de.ret_world.openrpgstudio.editor.helpers.Internationalization;
import de.ret_world.openrpgstudio.editor.script.ScriptObject;
import de.ret_world.openrpgstudio.mapeditor.scripts.Layer;
import de.ret_world.openrpgstudio.mapeditor.scripts.TileSet;
import java.awt.Insets;
import java.awt.Rectangle;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

/**
 * This Dialog is called by MapEditor for creating and editing TileMap Layers.
 *
 * @author Emu
 */
public class EditLayerDialog {
    
    private BFrame parent;
    private BDialog dialog;
    private Layer layer;
    private BTextField txtID;
    private BComboBox cbType;
    private BComboBox cbTilesets;
    private ImagePreviewWidget ipw;
    private boolean ok;
    
    public EditLayerDialog(BFrame parent) {
        this.parent = parent;
    }
    
    public boolean showDialog(Layer layer) {
        this.layer = layer;
        Internationalization intn = Internationalization.getInstance();
        Project project = Project.getInstance();
        LayoutInfo li = new LayoutInfo(LayoutInfo.WEST, LayoutInfo.HORIZONTAL, new Insets(5, 5, 5, 5), null);
        
        dialog = new BDialog(parent, intn.getString("ME.EDITLAYER"), true);
        
        ColumnContainer cc = new ColumnContainer();
        
        cc.add(new BLabel(intn.getString("ME.LAYERID")), li);
        txtID = new BTextField(20);
        txtID.setText(layer.getID());
        cc.add(txtID, li);
        cc.add(new BLabel(intn.getString("ME.LAYERTYPE")), li);
        cbType = new BComboBox(new String[]{"TILE", "EVENT"});
        cbType.addEventLink(ValueChangedEvent.class, this, "typeChanged");
        cc.add(cbType, li);
        
        cc.add(new BLabel(intn.getString("ME.TILESET")), li);
        
        HashMap<String, ScriptObject> scriptMap = Project.getInstance().getDatabase().getScriptMap(TileSet.TYPE);
        Collection<ScriptObject> lst;
        if (scriptMap == null) {
            lst = new ArrayList<>();
        } else {
            lst = scriptMap.values();
        }
        
        cbTilesets = new BComboBox(lst);
        cbTilesets.addEventLink(ValueChangedEvent.class, this, "tsChanged");
        cc.add(cbTilesets, li);
        
        ExplicitContainer ec = new ExplicitContainer();
        ipw = new ImagePreviewWidget(true);
        ec.add(ipw, new Rectangle(250, 200));
        cc.add(ec, new LayoutInfo(LayoutInfo.CENTER, LayoutInfo.BOTH, new Insets(5, 5, 5, 5), null));
        
        BorderContainer bc2 = new BorderContainer();
        BButton btn = new BButton(intn.getString("OK"));
        btn.addEventLink(CommandEvent.class, this, "ok");
        bc2.add(btn, BorderContainer.EAST, li);
        btn = new BButton(intn.getString("CANCEL"));
        btn.addEventLink(CommandEvent.class, this, "cancel");
        bc2.add(btn, BorderContainer.CENTER, li);
        cc.add(bc2, new LayoutInfo(LayoutInfo.EAST, LayoutInfo.NONE, new Insets(5, 5, 5, 5), null));
        
        tsChanged();
        
        dialog.setContent(cc);
        dialog.addEventLink(WindowClosingEvent.class, this, "cancel");
        dialog.setResizable(false);
        dialog.pack();
        dialog.setVisible(true);
        
        return ok;
    }
    
    private void cancel() {
        dialog.dispose();
    }
    
    private void ok() {
        layer.setID(txtID.getText());
        layer.setType(Layer.TYPE.valueOf((String) cbType.getSelectedValue()));
        if (cbType.getSelectedValue().equals("TILE")) {
            layer.setTileset((TileSet) cbTilesets.getSelectedValue());
        }
        
        ok = true;
        dialog.dispose();
    }
    
    private void typeChanged() {
        if (cbType.getSelectedValue().equals("TILE")) {
            cbTilesets.setEnabled(true);
            ipw.setEnabled(true);
        } else {
            cbTilesets.setEnabled(false);
            ipw.setEnabled(false);
        }
    }
    
    private void tsChanged() {
        if (cbTilesets.getSelectedValue() != null) {
            Project project = Project.getInstance();
            Media m = ((TileSet) cbTilesets.getSelectedValue()).getImage();
            Path p = project.getFolder("root").resolve(m.getFile());
            ipw.setImage(p);
        }
    }
}
